#include <math.h>
#include "sched_pub.h"

_Bool digital_filter(thing_model_t *model, void *parameter) {
    if (!model) return false;

    switch (model->value_type) {
        case vt_point:
        case vt_dpoint:
            return true;
        default:
            return false;
    }
}

_Bool analog_filter(thing_model_t *model, void *parameter) {
    if (!model) return false;

    switch (model->value_type) {
        case vt_normalized:
        case vt_scaled:
        case vt_floating:
            return true;
        default:
            return false;
    }
}

_Bool all_call_filter(thing_model_t *model, void *parameter) {
    if (!model) return false;

    switch (model->value_type) {
        case vt_point:
        case vt_dpoint:
        case vt_normalized:
        case vt_scaled:
        case vt_floating:
            return true;
        default:
            return false;
    }
}

_Bool totals_filter(thing_model_t *model, void *parameter) {
    if (!model) return false;

    switch (model->value_type) {
        case vt_totals:
            return true;
        default:
            return false;
    }
}

_Bool catalog_filter(thing_model_t *model, void *parameter) {
    if (!model) return false;

    return !parameter || model->catalog == *(data_catalog_e *) parameter;
}

_Bool true_compare(value_t *local, value_t *remote) {
    return local && remote;
}

_Bool value_changed_compare(value_t *local, value_t *remote, double max, double min) {
    if (!local || !remote)
        return false;

    //时间未变
    if (local->timestamp == remote->timestamp)
        return false;

    //超过范围值
    if (max > min && (remote->value < min || remote->value > max)) {
        return false;
    }

    //采集到数据
    if (0 == local->timestamp && 0 != remote->timestamp)
        return true;

    //值不相等
    if (fabsf(local->value - remote->value) > 0.01f)
        return true;

    return false;
}